using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.CompositeUI.SmartParts;
using BankTeller.BankTellerModule.Services;
using Microsoft.Practices.ObjectBuilder;

namespace BankTeller.CustomerMapExtensionModule
{
	/// <summary>
	/// Interaction logic for CustomerMap.xaml
	/// </summary>
	[SmartPart]
    public partial class CustomerMap : System.Windows.Controls.UserControl, ICustomerMap
    {
        const string mapUrlFormat = "http://maps.msn.com/home.aspx?strt1={0}&city1={2}&stnm1={3}&zipc1={4}";
        private bool mapLoaded = false;
        private ContextService contextService;

        [InjectionConstructor]
        public CustomerMap([ServiceDependency] ContextService contextService)
        {
            InitializeComponent();
            this.contextService = contextService;
        }

        [ServiceDependency]
        public ContextService ContextService
        {
            set { contextService = value; }
        }

        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            if (IsVisible && !mapLoaded)
            {
                LoadMap();
                mapLoaded = true;
            }
        }

        private void LoadMap()
        {
            if (contextService.CurrentCustomer != null)
            {
                string url = String.Format(mapUrlFormat, contextService.CurrentCustomer.Address1, contextService.CurrentCustomer.Address2, contextService.CurrentCustomer.City, contextService.CurrentCustomer.State, contextService.CurrentCustomer.ZipCode);
                browser.Navigate(Uri.EscapeUriString(url));
            }
        }
    }
}
